package com.opennetexam.manager.config;

import com.google.common.util.concurrent.ThreadFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.*;

/**
 * @Description 线程池自定义配置
 * @author 奶茶
 * Created by on 2018/12/17 9:00.
 */
@Configuration
public class ThreadPoolConfig {

    /**
     * 核心线程池大小
     */
    private static final Integer CORE_POOL_SIZE = Runtime.getRuntime().availableProcessors();

    @Bean(value = "customThreadPoolQueue")
    public ExecutorService customThreadPoolQueue(){
        ThreadFactory threadFactory = new ThreadFactoryBuilder()
                .setNameFormat("consumer-queue-thread-%d")
                .build();
        return new ThreadPoolExecutor(CORE_POOL_SIZE, CORE_POOL_SIZE + 1, 1, TimeUnit.SECONDS,
                new LinkedBlockingDeque<Runnable>(), threadFactory,new ThreadPoolExecutor.CallerRunsPolicy());
    }

}
